//
//  SLVideoPlayerController.m
//  PhotographVideoRecording
//
//  Created by 阿飞 on 2020/4/23.
//  Copyright © 2020 阿飞. All rights reserved.
//

#import "SLVideoPlayerController.h"
#import "CLPlayerView.h"
@interface SLVideoPlayerController ()

@property (nonatomic,weak) CLPlayerView *playerView;
@property (nonatomic,strong)NSURL *url;

@end

@implementation SLVideoPlayerController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self buttonVideoClicked];
    
    
}
-(void)buttonVideoClicked{
    
      CLPlayerView *playerView = [[CLPlayerView alloc] initWithFrame:CGRectMake(0,0, self.view.frame.size.width, self.view.frame.size.height)];
      self.playerView = playerView;
     
      [self.view addSubview:self.playerView];

      [self.playerView updateWithConfigure:^(CLPlayerViewConfigure *configure) {
          //重复播放，默认不播放
          configure.repeatPlay = YES;
          //当前控制器是否支持旋转，当前页面支持旋转的时候需要设置，告知播放器
          configure.isLandscape = NO;
          //顶部工具条隐藏样式，默认不隐藏
          configure.topToolBarHiddenType = TopToolBarHiddenNever;
      }];
      //视频地址
      _playerView.url = [NSURL fileURLWithPath:self.videoPath.path];
      //播放
      [_playerView playVideo];
      //返回按钮点击事件回调,小屏状态才会调用，全屏默认变为小屏
       __weak typeof(self) weakSelf = self;
      [_playerView backButton:^(UIButton *button) {
          __strong typeof(weakSelf) strongSelf = weakSelf;
          NSLog(@"返回按钮被点击");
          [strongSelf dismissViewControllerAnimated:NO completion:nil];
      }];
      
      [_playerView defineButton:^(UIButton *button) {
        __strong typeof(weakSelf) strongSelf = weakSelf;
        NSLog(@"返回按钮被点击");
        [strongSelf dismissViewControllerAnimated:NO completion:nil];
        [[NSNotificationCenter defaultCenter]postNotificationName:@"DismissVC" object:nil];
      }];
      
      //播放完成回调
      [_playerView endPlay:^{
          NSLog(@"播放完成");
      }];
}
-(void)viewDidDisappear:(BOOL)animated{
    
    [super viewDidDisappear:animated];
    [_playerView destroyPlayer];
    [[NSNotificationCenter defaultCenter]removeObserver:self name:@"DismissVC" object:nil];
}
@end
